<template>
  <div class="about">
    <el-button @click="onBLEDiscovery">蓝牙配对</el-button>
    <el-button @click="onUSBDiscovery">USB连接</el-button>
  </div>
</template>

<script lang="ts">


declare var navigator: { [x: string]: any; };
import {Options, Vue} from 'vue-class-component';

@Options({})
export default class LabelIndex extends Vue {
  private ble: any = navigator["bluetooth"];
  private usb: any = navigator["usb"];
  private deviceList = {};
  private bleCharacteristics = {};
  private serviceUUIDs = {};
  private currentServiceNum = 1;
  private continue = false;
  private currentServiceUUID = '';
  private characteristic: any = {};
  private targetDevice: any = {};

  onBLEDiscovery(): void {
    this.ble["requestDevice"]({
      acceptAllDevices: true,
      optionalServices: [0xfff0, 0xfee7, 0x1800, 0x1801, 0x180a, "49535343-fe7d-4ae5-8fa9-9fafd205e455", 0x180d, 0x181c, "0000aaa0-0000-1000-8000-aabbccddeeff"]
    }).then((device: any) => {
      device.gatt.connect().then((value: any) => {
        console.log(device.gatt.connected)
        value.getPrimaryService("49535343-fe7d-4ae5-8fa9-9fafd205e455").then((service: any) => {
          // "flags":10,"uuid":"495353436daa4d02abf619569aca69fe"		CHR_F_WRITE,	CHR_F_READ
          // "flags":24,"uuid":"49535343aca3481c91ecd85e28a60318"		CHR_F_NOTIFY,	CHR_F_WRITE
          // "flags":16,"uuid":"495353431e4d4bd9ba6123c647249616"		CHR_F_NOTIFY
          // "flags":12,"uuid":"49535343884143f4a8d4ecbe34729bb3"		CHR_F_WRITE, CHR_F_WRITE_NO_RSP
          service.getCharacteristic('49535343-8841-43f4-a8d4-ecbe34729bb3').then((characteristic: any) => {
            console.log(characteristic)
            // characteristic.getDescriptor(0x2A00).then(   (descriptor: any) => {
            //   console.log(descriptor)
            // }            )

            const encoder = new TextEncoder();
            const buffer = encoder.encode("BACKFEED 1000\r\n");
            characteristic.writeValueWithoutResponse(buffer);
            // characteristic.getDescriptor(0x2A27).then((descriptor: any) => {
            //   console.log(descriptor)
            //   descriptor.writeValue("HOME\r\n")
            // })
          })
        })
      })

    });
  }

  async onUSBDiscovery() {
    const device: any = await this.usb["requestDevice"]({filters: []});
    await device.open();
    console.log(device.opened);
    const {configurationValue, interfaces} = (device.configuration || device.configurations[0]);
    await device.selectConfiguration(configurationValue || 0);
    await device.claimInterface(interfaces[0].interfaceNumber || 0);

    let inEndpoint, outEndpoint;

    for (const {alternates} of interfaces) {
      const alternate = alternates[0];
      const USB_PRINTER_CLASS = 7;
      const USB_SCANNER_CLASS = 0;
      if (alternate.interfaceClass !== USB_SCANNER_CLASS) {
        continue;
      }

      for (const endpoint of alternate.endpoints) {
        if (endpoint.type !== "bulk") {
          continue;
        }

        if (endpoint.direction === "in") {
          inEndpoint = endpoint.endpointNumber;
        } else if (endpoint.direction === "out") {
          outEndpoint = endpoint.endpointNumber;
        }
      }
    }

    const cmd = new Uint8Array([0x1f, 0x1b, 0x1f, 0x67, 0x00]);
    device.transferOut(outEndpoint, cmd);
  }
}
</script>
<style scoped lang="less">

</style>